package com.czw.springmvc.resolver;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: SpringMVC_02
 * @description:
 * @author: Feri(邢朋辉)
 * @create: 2021-06-15 10:12
 */
public class MyExceptionResovler implements HandlerExceptionResolver {
    //异常处理器 方法一旦触发，说明出现了异常
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        //记录错误日志  记录到错误日志文件中

        ex.printStackTrace();
        //根据状态响应码 跳转不同的页面
        ModelAndView mv=new ModelAndView();
        switch (response.getStatus()){
            case 404:mv.setViewName("/error404.html");break;
            case 500:mv.setViewName("/error500.html");break;
            default:mv.setViewName("/error.html");break;
        }
        return mv;
    }
}
